package com.hxd.otc.module.wallet.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.wallet.adapter.OneCoinTypeDetailAdapter;
import com.hxd.otc.module.wallet.bean.WalletAccountInfoBean;
import com.hxd.otc.module.wallet.bean.WalletCoinRecordBean;
import com.hxd.otc.module.wallet.contract.WalletCoinDetailContract;
import com.hxd.otc.module.wallet.presenter.OneCoinTypeDetailPresenter;
import com.hxd.otc.widget.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 钱包详情页面
 */
public class OneCoinTypeDetailActivity extends BaseActivity<OneCoinTypeDetailPresenter> implements WalletCoinDetailContract.View {

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.tv_total)
    TextView tvTotle;

    @BindView(R.id.tv_total_cny)
    TextView tvTotleCny;

    @BindView(R.id.tv_available_totle)
    TextView tvAvailableTotle;

    @BindView(R.id.tv_freeze_totle)
    TextView tvFreezeTotle;


    OneCoinTypeDetailAdapter adapter;
    public static final int REQUEST_CODE_WITHDRAW = 1;//提币


    private boolean isRefresh;
    private int currentPage = 1;//当前页
    private int rows = 15;//每页显示条数
    //钱包首页传递进来
    private WalletAccountInfoBean accountInfo;
    private int pos;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_one_coin_type_detail;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                isRefresh = false;
                mPresenter.doLoadData(++currentPage, rows, accountInfo.getHoldCoinDetailVoList().get(pos).getCoinType());
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshData();
            }
        });
        initAdapter();
    }

    private void refreshData() {
        isRefresh = true;
        currentPage = 1;
        mPresenter.doLoadData(currentPage, rows, accountInfo.getHoldCoinDetailVoList().get(pos).getCoinType());
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new OneCoinTypeDetailAdapter();
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(OneCoinTypeDetailActivity.this, CoinRecordDetailActivity.class);
                    intent.putExtra("info", (WalletCoinRecordBean) adapter.getData().get(position));
                    startActivity(intent);
                }
            });
        }
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
//            accountInfo = (WalletAccountInfoBean) getIntent().getExtras().getSerializable("accountInfo");
//            pos = getIntent().getExtras().getInt("pos");
//            titleBar.setTitleText(accountInfo.getHoldCoinDetailVoList().get(pos).getCoinType());
//            tvTotle.setText(StringUtil.format8Number(accountInfo.getHoldCoinDetailVoList().get(pos).totalAmount));
//            tvTotleCny.setText(getString(R.string.wallet_assets_to_cny,
//                    StringUtil.format2Number(accountInfo.getHoldCoinDetailVoList().get(pos).totalLegalTenderAmount),
//                    accountInfo.getHoldCoinDetailVoList().get(pos).legalTenderUnit));
//            tvFreezeTotle.setText(StringUtil.format8Number(accountInfo.getHoldCoinDetailVoList().get(pos).getFrozenBlance()));
//            tvAvailableTotle.setText(StringUtil.format8Number(accountInfo.getHoldCoinDetailVoList().get(pos).getBalance()));
//            mRefreshLayout.autoRefresh(0, 10, 1);
    }

    @OnClick({R.id.tv_coin_recharge, R.id.tv_coin_withdraw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_coin_recharge:
                startActivity(CoinRechargeActivity.class);
                break;
            case R.id.tv_coin_withdraw:
                startActivity(CoinWithdrawActivity.class);
                break;
        }
    }


    private void startActivity(@NonNull Class clazz) {
        Intent intent = new Intent(this, clazz);
        intent.putExtra("coinType", accountInfo.getHoldCoinDetailVoList().get(pos).coinType);
        startActivity(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_WITHDRAW:
                if (resultCode == RESULT_OK) {
                    //转币成功后刷新
                    refreshData();
                }
                break;
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected OneCoinTypeDetailPresenter createPresenter() {
        return new OneCoinTypeDetailPresenter(this);
    }


    @Override
    public void getListData(List<WalletCoinRecordBean> list) {
        if (isRefresh) {
            mRefreshLayout.setEnableLoadMore(true);
            adapter.setNewData(list);
        } else {
            if (list == null || list.size() < rows) {
                mRefreshLayout.setEnableLoadMore(false);
            } else {
                mRefreshLayout.setEnableLoadMore(true);
                adapter.addData(list);
            }
        }
    }

    @Override
    public void onShowNoMore() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }
}
